package com.xwh.b2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwh.b2c.common.PageResult;
import com.xwh.b2c.common.Result;
import com.xwh.b2c.dto.GoodsPageQueryDTO;
import com.xwh.b2c.entity.Category;
import com.xwh.b2c.entity.Goods;
import com.xwh.b2c.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author xoox
 * @since 2025-07-10
 */
@Slf4j
@RestController
@Tag(name = "商品管理",description = "商品管理")
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @Operation(summary = "添加商品", description = "添加商品")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody Goods goods){
        log.info("添加商品信息:{}",goods);
        this.goodsService.save(goods);
        return Result.success();
    }

    // 幂等接口 使用PutMapping
    @Operation(summary = "修改商品", description = "修改商品")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody Goods goods){
        this.goodsService.updateById(goods);
        return Result.success();
    }

    @Operation(summary = "删除商品", description = "删除商品")
    @DeleteMapping("/delete")
    public Result<Object> delete(@RequestBody List<Long> ids){
        this.goodsService.removeBatchByIds(ids);
        return Result.success();
    }


    @Operation(summary = "根据ID获取商品", description = "根据ID获取商品")
    @GetMapping("{id}")
    public Result<Goods> get(@PathVariable("id") Long id){
        Goods goods = this.goodsService.getById(id);
        return Result.success(goods);
    }

    //todo 0710 仿照member的管理后台构建商品的管理后台
    //todo 0710 仿照member的管理后台修复分类管理后台中的显示/隐藏逻辑

    @Operation(summary = "商品分页查询",description = "商品分页查询")
    @GetMapping("/page")
    public PageResult<Goods> page(@ModelAttribute GoodsPageQueryDTO goodsPageQueryDTO){
        log.info("page被调用了");
        log.info("controller: dto:{}",goodsPageQueryDTO);
        IPage<Goods> page= this.goodsService.getPage(goodsPageQueryDTO);
        log.info("controller: page:{}",page);
        return PageResult.success(page);
    }

    @Operation(summary = "上架/下架设置", description = "根据goodsNo 设置上架下架")
    @PutMapping("enable/{goodsNo}")
    public Result<Object> enable(@PathVariable Long goodsNo){
        Goods goods = this.goodsService.getById(goodsNo);
        this.goodsService.enable(goods);
        return Result.success();
    }

}
